package com.attendance.controller;

import com.attendance.Utils.Converter.EmployeeConverter;
import com.attendance.Utils.PaginationUtil;
import com.attendance.model.Biz.BizEmployee;
import com.attendance.model.Employee;
import com.attendance.model.Form.EmployeeForm;
import com.attendance.model.Result.Response;
import com.attendance.model.Result.ResultCode;
import com.attendance.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by OPTIPLEX on 2016/11/29.
 */

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Response getAll(@RequestParam(defaultValue = "1") int offset, @RequestParam(defaultValue = "10") int limit,
                           @RequestParam(defaultValue = "id") String sort, @RequestParam(defaultValue = "asc") String order,
                           @ModelAttribute EmployeeForm employeeForm) throws Exception{
        Response response = new Response();
        PaginationUtil.preset(offset, 2, sort, order);
        Employee employee = EmployeeConverter.convert(employeeForm);
        List<BizEmployee> list = employeeService.getAll(employee);
        PageInfo pageInfo = new PageInfo(list);
        response.put("result", pageInfo);
        return response;
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public Response add(@ModelAttribute EmployeeForm employeeForm){
        Response response = new Response();
        try {
            Employee employee = EmployeeConverter.convert(employeeForm);
            employeeService.insertSelective(employee);
        } catch (Exception e) {
            return new Response(ResultCode.EMPLOYEE_ADD_ERROR);
        }
        return response;
    }

    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public Response delete(@ModelAttribute EmployeeForm employeeForm){
        Response response = new Response();
        try {
            Employee employee = EmployeeConverter.convert(employeeForm);
            employeeService.deleteById(employee.getId());
        } catch (Exception e) {
            return new Response(ResultCode.EMPLOYEE_DELETE_ERROR);
        }
        return response;
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Response update(@ModelAttribute EmployeeForm employeeForm){
        Response response = new Response();
        try {
            Employee employee = EmployeeConverter.convert(employeeForm);
            employeeService.updateSelectiveById(employee);
        } catch (Exception e) {
            return new Response(ResultCode.EMPLOYEE_UPDATE_ERROR);
        }
        return response;
    }

    @RequestMapping(value = "/current",method = RequestMethod.GET)
    public Response current(SecurityContextHolderAwareRequestWrapper request){
        Response response = new Response();
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        response.put("username",auth.getName());
        response.put("job", auth.getAuthorities());
        Employee employee = employeeService.getByName(auth.getName());
        if(employee != null){
            response.put("uid",employee.getId());
            response.put("sex",employee.getSex());
            response.put("name",employee.getName());
        }
        return response;
    }

}
